package com.example.mydy;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.mydy.model.Advertisement;
import com.example.mydy.utils.MockDataManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class AdvertisementArticleActivity extends AppCompatActivity {

    private static final String TAG = "AdArticleActivity";

    private TextView titleTextView;
    private TextView dateTextView;
    private ImageView imageView;
    private TextView contentTextView;
    private ImageButton btnBack;
    private FloatingActionButton fabShare;
    private Button btnVisitLink;
    
    private MockDataManager mockDataManager;
    private Advertisement currentAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertisement_article);

        // 初始化视图
        titleTextView = findViewById(R.id.article_title);
        dateTextView = findViewById(R.id.article_date);
        imageView = findViewById(R.id.article_image);
        contentTextView = findViewById(R.id.article_content);
        btnBack = findViewById(R.id.btn_back);
        fabShare = findViewById(R.id.fab_share);
        btnVisitLink = findViewById(R.id.btn_visit_link);
        
        mockDataManager = MockDataManager.getInstance();

        // 设置返回按钮
        btnBack.setOnClickListener(v -> finish());
        
        // 设置分享按钮
        fabShare.setOnClickListener(v -> shareAdvertisement());

        // 获取广告ID
        Intent intent = getIntent();
        int adId = intent.getIntExtra("ad_id", -1);

        if (adId != -1) {
            // 加载广告内容
            loadAdvertisement(adId);
        } else {
            // 显示默认内容
            displayDefaultContent();
            Toast.makeText(this, "广告内容不可用", Toast.LENGTH_SHORT).show();
        }
    }

    private void loadAdvertisement(int adId) {
        // 从MockDataManager获取广告详情
        currentAd = MockDataManager.getAdvertisementById(adId);
        
        if (currentAd != null) {
            Log.d(TAG, "加载广告: " + currentAd.getTitle());
            
            // 显示广告内容
            titleTextView.setText(currentAd.getTitle());
            contentTextView.setText(currentAd.getContent());
            
            // 显示日期
            if (currentAd.getCreatedAt() != null) {
                dateTextView.setText(currentAd.getDate());
                dateTextView.setVisibility(View.VISIBLE);
            } else {
                dateTextView.setVisibility(View.GONE);
            }
            
            // 在实际应用中，这里应该使用图片加载库如Glide或Picasso加载图片
            // 这里我们简单地设置一个默认图片作为示例
            imageView.setImageResource(R.drawable.default_avatar);
            
            // 设置链接按钮
            if (currentAd.getLink() != null && !currentAd.getLink().isEmpty()) {
                btnVisitLink.setVisibility(View.VISIBLE);
                btnVisitLink.setOnClickListener(v -> {
                    // 打开链接的逻辑
                    Toast.makeText(AdvertisementArticleActivity.this, 
                            "打开链接: " + currentAd.getLink(), 
                            Toast.LENGTH_SHORT).show();
                    
                    // 在实际应用中，这里应该使用Intent打开浏览器
                    // Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(currentAd.getLink()));
                    // startActivity(browserIntent);
                });
            } else {
                btnVisitLink.setVisibility(View.GONE);
            }
        } else {
            // 广告未找到，显示默认内容
            Log.e(TAG, "广告未找到: ID = " + adId);
            displayDefaultContent();
            Toast.makeText(this, "广告未找到", Toast.LENGTH_SHORT).show();
        }
    }

    private void displayDefaultContent() {
        titleTextView.setText("广告详情");
        contentTextView.setText("此广告内容不可用或已被删除。");
        imageView.setImageResource(R.drawable.default_avatar);
        dateTextView.setVisibility(View.GONE);
        btnVisitLink.setVisibility(View.GONE);
        currentAd = null;
    }
    
    private void shareAdvertisement() {
        if (currentAd == null) {
            Toast.makeText(this, "没有可分享的内容", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 创建分享Intent
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, currentAd.getTitle());
        
        // 构建分享内容
        String shareContent = currentAd.getTitle() + "\n\n" + 
                              currentAd.getContent() + "\n\n";
                              
        // 如果有链接，添加到分享内容中
        if (currentAd.getLink() != null && !currentAd.getLink().isEmpty()) {
            shareContent += "链接: " + currentAd.getLink();
        }
        
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent);
        
        // 启动分享Activity
        startActivity(Intent.createChooser(shareIntent, "分享广告"));
    }
}